using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;

namespace DAL
{
    public class tbDonHangDAL
    {
        private SqlConnection cn = null;
        public tbDonHangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbDonHangDAL(tbDonHangDAL donhang)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public void Insert(tbDonHang donhang)
        {
            SqlCommand cm = new SqlCommand("sp_tbDonHang_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@KhachHang", donhang.KhachHang);
            cm.Parameters.AddWithValue("@NgayDatHang", donhang.NgayDatHang);
            cm.Parameters.AddWithValue("@NgayGiaoHang", donhang.NgayGiaoHang);
            cm.Parameters.AddWithValue("@TriGia", donhang.TriGia);
            cm.Parameters.AddWithValue("@TinhTrangGH", donhang.TinhTrangGH);
            cm.Parameters.AddWithValue("@PhuongThucThanhToan", donhang.PhuongThucThanhToan);
            cm.Parameters.AddWithValue("@TinhTrangThanhToan", donhang.TinhTrangThanhToan);
            cm.Parameters.AddWithValue("@NhanVienGiaoHang", donhang.NhanVienGiaoHang);
            cm.Parameters.AddWithValue("@DiaChiGH", donhang.DiaChiGH);
            cm.ExecuteNonQuery();
        }
        public void Update(tbDonHang donhang)
        {
            SqlCommand cm = new SqlCommand("sp_tbDonHang_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDH", donhang.MaDH);
            cm.Parameters.AddWithValue("@KhachHang", donhang.KhachHang);
            cm.Parameters.AddWithValue("@NgayDatHang", donhang.NgayDatHang);
            cm.Parameters.AddWithValue("@NgayGiaoHang", donhang.NgayGiaoHang);
            cm.Parameters.AddWithValue("@TriGia", donhang.TriGia);
            cm.Parameters.AddWithValue("@TinhTrangGH", donhang.TinhTrangGH);
            cm.Parameters.AddWithValue("@PhuongThucThanhToan", donhang.PhuongThucThanhToan);
            cm.Parameters.AddWithValue("@TinhTrangThanhToan", donhang.TinhTrangThanhToan);
            cm.Parameters.AddWithValue("@NhanVienGiaoHang", donhang.NhanVienGiaoHang);
            cm.Parameters.AddWithValue("@DiaChiGH", donhang.DiaChiGH);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_tbDonHang_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDH", iD);
            cm.ExecuteNonQuery();
        }
        public tbDonHangCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_tbDonHang_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbDonHangCollection dhCollection = new tbDonHangCollection();

            while (dr.Read())
            {
                tbDonHang donhang = new tbDonHang();
                donhang.MaDH = int.Parse(dr.GetValue(0).ToString());
                donhang.KhachHang = int.Parse(dr.GetValue(1).ToString());
                donhang.NgayDatHang = DateTime.Parse(dr.GetValue(2).ToString());
                donhang.NgayGiaoHang = DateTime.Parse(dr.GetValue(3).ToString());
                donhang.TriGia = int.Parse(dr.GetValue(4).ToString());
                donhang.TinhTrangGH = int.Parse(dr.GetValue(5).ToString());
                donhang.PhuongThucThanhToan = int.Parse(dr.GetValue(6).ToString());
                donhang.TinhTrangThanhToan = int.Parse(dr.GetValue(7).ToString());
                donhang.NhanVienGiaoHang = int.Parse(dr.GetValue(8).ToString());
                donhang.DiaChiGH = int.Parse(dr.GetValue(9).ToString());
                dhCollection.Add(donhang);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return dhCollection;
        }

        private object GetValue(int p)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
}
